package com.jianyun.jyzs.widget;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.PopupWindow;

import com.jianyun.jyzs.MainActivity;
import com.jianyun.jyzs.R;

import io.rong.imkit.utils.NotificationUtil;


/**
 * Created by yangshijun on 2018/10/25.
 */

public class NotificationUtils extends ContextWrapper {

    private final Context mContext;
    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    private NotificationCompat.Builder builderCompat;
    private Notification.Builder builder;
    private static final int NO_3 = 0x3;

    public NotificationUtils(Context context) {
        super(context);
        this.mContext = context;
    }

    public void initBuilder(String title, String content, int progress) {
        if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel();
            builder = getChannelNotification(title, content, progress);
        } else {
            builderCompat = getNotification_25(title, content, progress);
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }

    private NotificationManager getManager() {
        if (manager == null) {
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }

    @SuppressLint("WrongConstant")
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(String title, String content, int progress) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        return new Notification.Builder(getApplicationContext(), id)
                .setContentTitle("正在更新...") //设置通知标题
                .setSmallIcon(R.drawable.rce_ic_logo1)
                .setContentIntent(contentIntent)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.rce_ic_logo1)) //设置通知的大图标
                .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级：最大
                .setAutoCancel(true)//设置通知被点击一次是否自动取消
                .setContentText("下载进度:" + "100%")
                .setProgress(100, 100, false);

    }

    public NotificationCompat.Builder getNotification_25(String title, String content, int progress) {
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.rce_ic_logo1);
        builder.setContentTitle("下载");
        builder.setContentText("正在下载");
        builder.setContentIntent(contentIntent);
        getManager().notify(NO_3, builder.build());
        builder.setProgress(100, 0, false);
        return builder;

    }

    public void sendNotification(String title, String content, int progress) {
        if (Build.VERSION.SDK_INT >= 26) {
            if (builder != null) {
                builder.setContentText("下载进度:" + progress + "%");
                builder.setProgress(100, progress, false);
                Notification notification = builder.build();
                getManager().notify(NO_3, notification);
                Log.i("test", "进度：" + progress);
            }

        } else {
            if (builderCompat != null) {
                builderCompat.setProgress(100, progress, false);
                getManager().notify(NO_3, builderCompat.build());
                //下载进度提示
                builderCompat.setContentText("下载" + progress + "%");


            }

        }
    }

    public void show3(int progress) {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.rce_ic_logo1);
        builder.setContentTitle("下载");
        builder.setContentText("正在下载");
        final NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(NO_3, builder.build());
        builder.setProgress(100, 0, false);
        //下载以及安装线程模拟
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    builder.setProgress(100, i, false);
                    manager.notify(NO_3, builder.build());
                    //下载进度提示
                    builder.setContentText("下载" + i + "%");
                    try {
                        Thread.sleep(50);//演示休眠50毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //下载完成后更改标题以及提示信息
                builder.setContentTitle("开始安装");
                builder.setContentText("安装中...");
                //设置进度为不确定，用于模拟安装
                builder.setProgress(0, 0, true);
                manager.notify(NO_3, builder.build());
//                manager.cancel(NO_3);//设置关闭通知栏
            }
        }).start();
        Log.i("test","进度："+progress);
    }


    public void cancelNotification() {
        getManager().cancel(NO_3);
    }


}
